package com.hyl.crmclient.intercept;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Description TODO
 * @Author hyl
 * @Date 2022/5/31 16:18
 **/
public class LoginIntercept implements HandlerInterceptor {
	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		System.out.println("拦截器 请求执行之 前");
		//判断用户是否存在==》session
		/*if(request.getSession().getAttribute("loginuser") == null){ //用户未登入
			response.setCharacterEncoding("utf-8");
			response.setContentType("text/html;charset=UTF-8");
			PrintWriter out = response.getWriter();
			out.print("<script>" +
					"alert('请登入');" +
					"location.href='jsp/login.jsp';" +
					"</script>");
			out.flush();
			out.close();
			return false;
		}else{//已登入
			//进入用户实际请求的==》controller
			return true;
		}*/
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		System.out.println("拦截器 请求执行之 后");
	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
		System.out.println("拦截器 视图渲染完成之后");
	}
}
